
window.Global = {
    player: null,
    game: null,
    wxEnvironment: false
}

cc.Class({
    extends: cc.Component,

   properties: {
        gameoverLabel : {
            default: null,
            type: cc.Node
        },
        startLabel : {
            default: null,
            type: cc.Node
        },

        // 这个属性引用了星星预制资源
        starPrefab: {
            default: null,
            type: cc.Prefab
        },
        // 星星产生后消失时间的随机范围
        maxStarDuration: 0,
        minStarDuration: 0,
        // 地面节点，用于确定星星生成的高度
        ground: {
            default: null,
            type: cc.Node
        },
        // player 节点，用于获取主角弹跳的高度，和控制主角行动开关
        player: {
            default: null,
            type: cc.Node
        },
        //得分节点
        score:0,
        scoreDisplay: {
            default: null,
            type: cc.Label
        },
        timer : 0,

        //上一次的星星位置 不能离的太近
        starX : 0,
        starDistance : 100,

        //todo 得分的声音
        scoreAudio: {
            default: null,
            url: cc.AudioClip
        },
    },

    onLoad: function(){
        Global.player = this.player.getComponent('Player');
        Global.game   = this;

        if (Global.wxEnvironment) {
            wx.startAccelerometer();
        }
    },

    cocosLoad: function () {
        this.score = 0;

        // 获取地平面的 y 轴坐标
        this.groundY = this.ground.y + this.ground.height/2;

        this.groundY = this.player.y - this.player.height/2;

        // 生成一个新的星星
        this.spawnNewStar();

        //分数置0
        this.score = 0;
        this.scoreDisplay.string = "Score:0";
    },

   //生成一个新的星星
    spawnNewStar: function() {
        // 使用给定的模板在场景中生成一个新节点
        var newStar = cc.instantiate(this.starPrefab);
        // 将新增的节点添加到 Canvas 节点下面
        this.node.addChild(newStar);
        // 为星星设置一个随机位置
        newStar.setPosition(this.getNewStarPosition());

        newStar.getComponent('Star').game = this;

        this.starDuration = this.minStarDuration + cc.random0To1() * (this.maxStarDuration - this.minStarDuration);
        // 初始化计时器
        this.timer = 0;
    },

    //随机生成星星的位置
    getNewStarPosition: function () {
        var randX = 0;
        // 根据地平面位置和主角跳跃高度，随机得到一个星星的 y 坐标
        var randY = this.groundY + cc.random0To1() * Global.player .jumpHeight + 50;
        if (randY < 50) randY = 50;
        // 根据屏幕宽度，随机得到一个星星 x 坐标
        var maxX = this.node.width/2;

        randX = cc.randomMinus1To1() * maxX; 
        if (Math.abs(randX-this.starX) < this.starDistance) return this.getNewStarPosition();
        this.starX = randX;
        // 返回星星坐标
        return cc.p(randX, randY);
    },

    //加分
    gainScore: function(addScroe) {
        addScroe = addScroe  || 1;
        this.score += addScroe;
        this.scoreDisplay.string = "Score:" + this.score.toString();
        cc.audioEngine.playEffect(this.scoreAudio, false);
    },

    //游戏结束
    gameOver : function() {
        //移除水平动作
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, Global.player.onKeyDown, Global.player);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, Global.player.onKeyUp, Global.player);
        cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION,this.onDeviceMotion, Global.player);
        console.log("gameover");
        Global.player.xSpeed = 0;
        Global.player.accLeft = false;
        Global.player.accRight = false;
        
        //移除重复的跳跃动作
        this.player.stopAction(Global.player.jumpAction);

        //显示游戏结束和开始按钮
        this.gameoverLabel.active = true;
        this.startLabel.active = true;


        if (Global.wxEnvironment) {
            wx.stopAccelerometer();
        }
    }

});
